﻿@model order_dto
@{
    ViewBag.Title = "订单详情";
    Layout = "~/Views/Shared/_MallLayout.cshtml";
    var img_url = ViewBag.img_url;
    string wxOpenId = ViewBag.WXOpenId;
}
<body class="page-cartConfirm page-order page-MyorderView">
    <input type="hidden" id="wxOpenId" value="@wxOpenId" />

    <div class="wait">
        <div class="states">
            <div class="fn-clear">
                <i class="icon-state1 fn-left"></i>
                @if (Model.status == OrderStatus.WaitPay) {
                    <span class="fn36 fn-left">
                        待支付
                    </span>
                } else if (Model.status == OrderStatus.WaitDelivery) {
                    <span class="fn36 fn-left">
                        待发货
                    </span>
                } else if (Model.status == OrderStatus.Confirm) {
                    <span class="fn36 fn-left">
                        待收货
                    </span>
                } else if (Model.status == OrderStatus.Finish) {
                    <span class="fn36 fn-left">
                        已完成
                    </span>
                } else if (Model.status == OrderStatus.Close) {
                    <span class="fn36 fn-left">
                        已关闭
                    </span>
                } else if (Model.status == OrderStatus.Delete) {
                    <span class="fn36 fn-left">
                        已删除
                    </span>
                }
                @if (Model.status == OrderStatus.Confirm) {
                    if (Model.shipping.shipping_time != null
                        && ((DateTime)Model.shipping.shipping_time).AddDays(5) < DateTime.Now
                        && ((DateTime)Model.shipping.shipping_time).AddDays(12) > DateTime.Now) {
                        <span class="fn-right fn26 count_down">
                            <em class="day_num"></em><em class="hour_num"></em><em class="min_num"></em><em class="sec_num"></em>
                            后自动收货
                        </span>
                    }
                }
            </div>

        </div>
        <div class="mod-orderDetail bg-white relative mb10">
            <div class="orderAddress absolute">
                @if (Model.status == OrderStatus.Confirm || Model.status == OrderStatus.Finish) {<div class="box fn28" onclick="javascript: location.href='/myorder/OrderLogistics?order_id=@Model.id'">
                        <i class="icon-site"></i>
                        查看物流信息
                        <i class="icon-go"></i>
                    </div>}
                <div class="box">
                    <i class="icon-loca"></i>
                    <div class="top fn30 mb20">
                        <span class="name">@Model.shipping.ship_to</span>&nbsp;
                        @Model.shipping.phone
                    </div>
                    <p class="col-gray fn26 inAddress">  @Model.shipping.region_name  @Model.shipping.address</p>
                </div>
            </div>
            <div class="kong"></div>
            <ul>
                <li>
                    @foreach (var item in Model.order_items) {
                        <div class="innerItem innerItemWhite">
                            <div class="img">
                                @if (item.order_item_type != 6) {
                                    <img src="@(item.product.main_image_url != null ? item.product.main_image_url : img_url + item.product.first_image)" />
                                } else {
                                    <img src="@(img_url +item.bazaar_product.main_img)" />
                                }
                            </div>
                            <div class="text">
                                <p class="fn28 col-gray1 tit mb15">
                                    @if (item.order_item_type != 6) {@item.product.name} else {@item.bazaar_product.product_name}
                                </p>
                                <div class="col-gray mb10">
                                    <span class="mr20">@item.sku_name</span>
                                    @*<span class="">XL</span>*@
                                </div>
                                <div class="price">
                                    @if (item.unit_price > 0) {
                                        <em class="fn22">￥</em>
                                        <span class="fn28 itemPrice">@item.unit_price.ToString("0.##")</span>
                                    }
                                    @if (item.unit_price > 0 && item.red_drill > 0) {
                                        <em class="fn22">+</em>
                                    }
                                    @if (item.red_drill > 0) {
                                        <span class="fn28 itemPrice">@item.red_drill.ToString("0.######")</span>
                                        <em class="fn22">红钻</em>
                                    }
                                    <span class="fn-right quantity">
                                        <em class="col-gray">×</em> <em class="j-quantity">@item.quantity</em>
                                    </span>
                                </div>
                            </div>
                        </div>
                    }

                    <div class="block fn-center borderTop mt30">
                        <i class="icon-phone2 mr10"></i>
                        <span class="fn28 contract">联系商家</span>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <div class="lt-block col-gray fn28">
        @if (Model.status == OrderStatus.WaitDelivery || Model.status == OrderStatus.Confirm || Model.status == OrderStatus.Finish) {

            <p>
                订 单 号：
                @Model.order_no
            </p>
            <p>
                支付单号：@Model.pay_info.pay_no
            </p>
            <p>
                创建时间：@(Model.order_time == null ? "" : Model.order_time.ToString("yyyy-MM-dd HH:mm:ss"))
            </p>
            <p>
                支付时间：@(Model.pay_info.create_time == null ? "" : Model.pay_info.create_time.ToString("yyyy-MM-dd HH:mm:ss"))
            </p>
            <p>
                @if (Model.status == OrderStatus.Confirm) {
                    <text>  发货时间： @(Model.shipping.shipping_time == null ? "" : ((DateTime)Model.shipping.shipping_time).ToString("yyyy-MM-dd HH:mm:ss"))</text>
                }
                @if (Model.status == OrderStatus.Finish) {
                    <text>成交时间： @(Model.finish_time == null ? "" : ((DateTime)Model.finish_time).ToString("yyyy-MM-dd HH:mm:ss"))</text>
                }
            </p>


        }

        @if (Model.status == OrderStatus.WaitPay) {
            <div class="btnBox flex">
                <button class=" ui-btn ui-btn-buy shadow cancel flex-1" type="button" value="@Model.id">取消订单</button>
                <button class="ui-btn ui-btn-buy shadow confirmbtn pay flex-1" type="button" value="@Model.order_no">立即支付</button>
            </div>
        } else if (Model.status == OrderStatus.Finish || Model.status == OrderStatus.Close) {
            <div class="btnBox flex">
                <button class=" ui-btn ui-btn-buy shadow confirmbtn delete flex-1" type="button" value="@Model.id">删除订单</button>
            </div>
        } else if (Model.status == OrderStatus.Confirm) {
            <div class="btnBox flex">
                <button class=" ui-btn ui-btn-buy shadow confirmbtn receipt flex-1" type="button" value="@Model.id">确认收货</button>
            </div>
        }



    </div>
    <script src="~/Scripts/tokenmall/countDown.js"></script>
    <script type="text/javascript">
            function isWxBrowser() {
            var result = false;
            //判断浏览器，在微信内打开，提示请在其他浏览器中打开
            var browser = {
                versions: function () {
                    var u = navigator.userAgent, app = navigator.appVersion;
                    return {
                        //移动终端浏览器版本信息
                        trident: u.indexOf('Trident') > -1, //IE内核
                        presto: u.indexOf('Presto') > -1, //opera内核
                        webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                        gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                        mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                        ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                        android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                        iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                        iPad: u.indexOf('iPad') > -1, //是否iPad
                        webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部,
                        WeiXin: u.match(/MicroMessenger/i) == "MicroMessenger" //是否为微信客户端
                    };
                }(),
                language: (navigator.browserLanguage || navigator.language).toLowerCase()
            };
            if (browser.versions.mobile) {
                //判断是否是移动设备打开。browser代码在下面
                var ua = navigator.userAgent.toLowerCase();
                //获取判断用的对象
                if (ua.match(/MicroMessenger/i) == "micromessenger") { //微信中打开
                    return true;
                }
            }
            return result;
        }
        $(function () {
            var wxOpenId = $("#wxOpenId").val();
            if (isWxBrowser() == true && (wxOpenId == undefined || wxOpenId == "")) {
                window.location.href = '@Url.Action("GetWXOpenIdAuth", "CommonTokenMall")?returnUrl=@System.Web.HttpUtility.UrlEncode(Request.Url.ToString())';
            }
            if (isWxBrowser() == false && wxOpenId != undefined && wxOpenId != "") {
                $("#wxOpenId").val('');
                wxOpenId = "";
            }
        })

        function onBridgeReady(wcPayModel) {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    "appId": wcPayModel.appid,
                    "timeStamp": wcPayModel.timestamp,
                    "nonceStr": wcPayModel.noncestr,
                    "package": wcPayModel.package,
                    "signType": "MD5",
                    "paySign": wcPayModel.paysign
                },
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                        window.location.href = '@Url.Action("OrderList", "MyOrder")';
                    } else {
                        window.location.href = '@Url.Action("OrderList", "MyOrder")';
                    }
                }
            );
        }
    </script>
    <script language="javascript" type="text/javascript">

        $(function () {
            $(".count_down").countDown({
                startTimeStr: "@(Model.shipping.shipping_time ?? DateTime.Now)",//开始时间
                endTimeStr: " @((Model.shipping.shipping_time ?? DateTime.Now.AddDays(-12)).AddDays(12))",//结束时间
                daySelector: ".day_num",
                hourSelector: ".hour_num",
                minSelector: ".min_num",
                secSelector: ".sec_num"
            });
        });


        $('.cancel').click(function () {
            var _this = this;
            swal({
                title: "您确定取消订单吗？",
                //text: "您确定取消订单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
                //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
            },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: '/MyOrder/CanelOrder', type: "post", data: { id: _this.value },
                            success: function (response) {
                                if (response.result_code == 1000) {
                                    window.location.reload();
                                }
                                else {
                                    layer.msg("你的订单取消失败");
                                }

                            }
                        });
                    } else {
                        layer.msg("你的订单取消失败");
                    }

                });
        });

        $('.pay').click(function () {
            var _this = this;
            var rqModel = {
                order_no: _this.value,
                wx_open_id: $("#wxOpenId").val()
            };
            swal({
                title: "您是否支付该订单吗？",
                //text: "您确定取消订单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
                //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
            },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: '/Order/WaitPay',
                            type: "post",
                            async: false,
                            data: rqModel,
                            success: function (response) {
                                if (response.result_code == 1000) {
                                    if (isWxBrowser() == true) {//微信浏览器打开，公众号支付
                                        //layer.msg("请在微信外浏览器打开完成支付");
                                        //return false;
                                        var wcPayModel = $.parseJSON(response.result_msg);
                                        if (typeof WeixinJSBridge == "undefined") {
                                            if (document.addEventListener) {
                                                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                            } else if (document.attachEvent) {
                                                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                            }
                                        } else {
                                            onBridgeReady(wcPayModel);
                                        }
                                    } else {
                                            location.href = response.result_msg;
                                    }
                                } else {
                                    layer.msg(response.result_msg);
                                }
                            }
                        });
                    } else {
                        layer.msg("支付订单失败");
                    }

                });
        });

        $('.receipt').click(function () {
            var _this = this;
            swal({
                title: "您确认收货吗？",
                //text: "您确定取消订单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
                //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
            },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: '/MyOrder/ConfirmOrder', type: "post", data: { id: _this.value },
                            success: function (response) {
                                if (response.result_code == 1000) {
                                    window.location.reload();
                                }
                                else {
                                    layer.msg("你确认收货失败");
                                }

                            }
                        });
                    } else {
                        layer.msg("你确认收货失败");
                    }

                });
        });


        $('.delete').click(function () {
            var _this = this;
            swal({
                title: "您确定删除订单吗？",
                //text: "您确定取消订单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: true
                //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
            },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: '/MyOrder/DeleteOrder', type: "post", data: { id: _this.value },
                            success: function (response) {
                                if (response.result_code == 1000) {
                                    history.go(-2);
                                }
                                else {
                                    layer.msg("你的订单删除失败");
                                }

                            }
                        });
                    } else {
                        layer.msg("你的订单删除失败");
                    }

                });
        });

        $('.contract').click(function () {
            location.href = "tel:@ViewBag.ServicePhone";
            @*var _this = this;
            swal({
                title: "您确定要拨打商家电话吗？",
                //text: "您确定取消订单吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
                //设置closeOnConfirm: false，下面二次确认按钮被点击后才会关闭模态框
            },
                function (isConfirm) {
                    if (isConfirm) {
                        location.href = "tel:@ViewBag.ServicePhone";
                    } else {
                        swal({
                            title: "商家！",
                            text: "商家拨号失败",
                            type: "error"
                        });
                    }

                });*@
        });
    </script>
</body>
